package com.cr.sync.service.impl;

import com.cr.sync.domain.TbSyncSuperviseViolationLawRegulation;
import com.cr.sync.mapper.TbSyncSuperviseViolationLawRegulationMapper;
import com.cr.sync.service.ITbSyncSuperviseViolationLawRegulationService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 远程数据-违法违规案件Service业务层处理
 * 
 * @author tang
 * @date 2025-08-29
 */
@Service
public class TbSyncSuperviseViolationLawRegulationServiceImpl implements ITbSyncSuperviseViolationLawRegulationService 
{

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    @Override
    public void insertBatch(List<TbSyncSuperviseViolationLawRegulation> list) {
        if(list != null && !list.isEmpty()) {

            SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
            TbSyncSuperviseViolationLawRegulationMapper mapper = sqlSession.getMapper(TbSyncSuperviseViolationLawRegulationMapper.class);

            try {
                // 先删除旧数据
                mapper.deleteAll();

                // 添加新数据
                for(int i = 0; i < list.size(); i ++) {
                    mapper.insertTbSyncSuperviseViolationLawRegulation(list.get(i));

                    // 每500条提交一次事务
                    if (i > 0 && i % 500 == 0) {
                        sqlSession.flushStatements();
                        sqlSession.clearCache();
                    }
                }

                sqlSession.commit();
            } catch (Exception e) {
                sqlSession.rollback();
                throw new RuntimeException(e);
            } finally {
                sqlSession.close();
            }
        }
    }

}
